//
//  UIColor+HexColor.m
//  HEMS
//
//  Created by TeemoYang on 2023/8/28.
//

#import "UIColor+HexColor.h"

@implementation UIColor (HexColor)

+ (UIColor *)colorWithHex:(NSString *)hexString {
    // 去除可能的 "#" 字符
    if ([hexString hasPrefix:@"#"]) {
        hexString = [hexString substringFromIndex:1];
    }

    // 将十六进制字符串转换为整数
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned hexValue;
    [scanner scanHexInt:&hexValue];

    // 提取红、绿、蓝分量
    CGFloat red = ((hexValue & 0xFF0000) >> 16) / 255.0;
    CGFloat green = ((hexValue & 0x00FF00) >> 8) / 255.0;
    CGFloat blue = (hexValue & 0x0000FF) / 255.0;

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

+ (UIColor *)colorWithAHex:(NSString *)hexString {
    if ([hexString hasPrefix:@"#"]) {
        hexString = [hexString substringFromIndex:1];
    }

    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned hexValue;
    [scanner scanHexInt:&hexValue];

    CGFloat alpha = ((hexValue >> 24) & 0xFF) / 255.0;
    CGFloat red = ((hexValue >> 16) & 0xFF) / 255.0;
    CGFloat green = ((hexValue >> 8) & 0xFF) / 255.0;
    CGFloat blue = (hexValue & 0xFF) / 255.0;

    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end
